package com.smarttiger.zero.admin.service;

import com.alibaba.fastjson.JSON;
import com.smarttiger.zero.admin.dao.MenuDao;
import com.smarttiger.zero.admin.entity.MenuModel;
import com.smarttiger.zero.admin.util.ParamNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class MenuService extends BaseService<MenuModel>{
    @Autowired
    MenuDao menuDao;

    @PostConstruct
    public void initMethod(){
        super.setDao(menuDao);
    }

    @ParamNames
    public List<MenuModel> getMenus(){
        return menuDao.getMenuModelsByParentIdIsNull();
    }

    @Transactional
    public List<MenuModel> saveMenus(Map<String,Object> map){
        menuDao.deleteAllMenu();
        MenuModel menu = JSON.parseObject(JSON.toJSONString(map), MenuModel.class);
        menuDao.save(menu);
        return getMenus();
    }

    public void resetId(MenuModel menu){
        menu.setId(UUID.randomUUID().toString());
        if(menu.getMenuModelList()!= null && menu.getMenuModelList().size()>0){
            menu.getMenuModelList().forEach(e -> resetId(e));
        }
    }

}
